// @Author EthanScriptOn
// @Desc
package pojo

import (
	"fmt"
	"gitee.com/fatzeng/srf_switch_dynamic_logic_director/component"
	"time"
)

type SrfSwitchUserInformation struct {
	Id          int64     `xorm:"bigint autoincr pk" json:"id" comment:"主键"`
	UserId      string    `xorm:"varchar(255) notnull unique" json:"userid" comment:"用户ID"`
	Username    string    `xorm:"varchar(255) notnull" json:"username" comment:"用户名"`
	Email       string    `xorm:"varchar(255) notnull" json:"email" comment:"邮箱地址"`
	Password    string    `xorm:"varchar(255) notnull" json:"password" comment:"密码"`
	Name        string    `xorm:"varchar(255) notnull" json:"name" comment:"姓名"`
	AvatarURL   string    `xorm:"text" json:"avatar_url" comment:"头像URL"`
	CreatedAt   time.Time `xorm:"created" json:"created_at" comment:"创建时间"`
	UpdatedAt   time.Time `xorm:"updated" json:"updated_at" comment:"更新时间"`
	IsActive    int       `xorm:"default 1" json:"is_active" comment:"是否激活(1-已经激活 2-未激活)"`
	IsDeleted   int       `xorm:"default 2" json:"is_deleted" comment:"是否已删除(1-已经删除 2-未删除)"`
	LastLoginAt time.Time `xorm:"timestamp" json:"last_login_at" comment:"最后登录时间"`
}

func (SrfSwitchUserInformation) TableName() string {
	return "srf_switch_user_information"
}

func GenerateSrfSwitchUserInformation(userId string, userName string, email string, password string, name string, avatarUrl string, isActive int, isDeleted int) *SrfSwitchUserInformation {
	return &SrfSwitchUserInformation{
		UserId:    userId,
		Username:  userName,
		Email:     email,
		Password:  password,
		Name:      name,
		AvatarURL: avatarUrl,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		IsActive:  isActive,
		IsDeleted: isDeleted,
	}
}

func GenerateSrfSwitchUserInformationWithEncryption(userId string, userName string, email string, password string, name string, avatarUrl string, isActive int, isDeleted int) (err error, instance *SrfSwitchUserInformation) {
	err, hashedPassword := component.PasswordEncryption(password)
	if err != nil {
		return fmt.Errorf("password encryption failed: %v", err), nil
	}
	return nil, GenerateSrfSwitchUserInformation(userId, userName, email, hashedPassword, name, avatarUrl, isActive, isDeleted)
}

func GenerateSrfSwitchUserInformationWithOnline(userId string, userName string, email string, password string, name string, avatarUrl string) (err error, instance *SrfSwitchUserInformation) {
	return GenerateSrfSwitchUserInformationWithEncryption(userId, userName, email, password, name, avatarUrl, 1, 2)
}
